<template>
  <div class="areaContainer">
    <div class="option">
      <el-button
        type="success"
        size="small"
        @click="addArea()"
      >新增</el-button>
      <el-button
        type="primary"
        size="small"
        @click="Refresh()"
      >刷新</el-button>
    </div>
    <el-table
      :data="List"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="index"
        label="序号"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="运营商"
      ></el-table-column>
      <el-table-column
        prop="content"
        label="消息内容"
      ></el-table-column>
      <el-table-column
        prop="city"
        label="城市"
      ></el-table-column>
      <el-table-column
        prop="areaCode"
        label="区号"
      ></el-table-column>

      <el-table-column
        fixed="right"
        label="操作"
        width="240"
      >
        <template slot-scope="scope">
          <!-- <el-button @click="handleClick(scope.row)" type="primary" size="small">查看</el-button> -->
          <el-button
            @click="EditClick(scope.row)"
            type="primary"
            plain
            size="small"
          >编辑</el-button>
          <el-button
            @click="DeleteClick(scope.row)"
            type="danger"
            size="small"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="paginat">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage4"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 弹框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <!-- <div class="row">
        <span class="rowTil">运营商:</span>
        <el-select
          v-model="carrierOperatorId"
          placeholder="请选择"
        >
          <el-option
            v-for="item in carrierList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>

      <div class="row">
        <span class="rowTil">城市:</span>
        <el-input
          v-model="city"
          placeholder="请输入城市"
        ></el-input>
      </div>

      <div class="row">
        <span class="rowTil">区号:</span>
        <el-input
          v-model="areaCode"
          placeholder="请输入区号"
        ></el-input>
      </div>

      <div class="row">
        <span class="rowTil">内容:</span>
        <el-input
          v-model="content"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="isOkClick()"
        >确 定</el-button>
      </span> -->

      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
        class="demo-ruleForm"
      >

        <el-form-item
          label="运营商"
          prop="carrierOperatorId"
        >
          <el-select
            v-model="ruleForm.carrierOperatorId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in carrierList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          label="城市"
          prop="city"
        >
          <el-input
            v-model.number="ruleForm.city"
            placeholder="城市名称"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="区号"
          prop="areaCode"
        >
          <el-input
            v-model.number="ruleForm.areaCode"
            placeholder="请输入区号"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="内容"
          prop="content"
        >
          <el-input
            v-model.number="ruleForm.content"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>

        <div style="text-align:right;">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="isOkClick('ruleForm')"
          >确 定</el-button>
        </div>

      </el-form>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      List: [],
      total: "",
      total: 0,
      val: 1, //当前多少页
      currentPage4: 1,
      flag: 0, //判断是 编辑还是添加的标识
      editId: 0, //编辑id
      dialogVisible: false,
      carrierList: [], //运营商name数组

      ruleForm: {
        carrierOperatorId: "", //运营商id
        areaCode: "",
        city: "",
        content: ""
      },
      rules: {
        carrierOperatorId: [{ required: true, message: "请选择运营商" }],
        areaCode: [
          { required: true, message: "请输入区号" },
          { type: "number", message: "请输入数字" }
        ],
        city: [{ required: true, message: "请输入城市" }],
        content: [{ required: true, message: "请输入内容" }]
      }
    };
  },
  created() {
    this.getList();
    this.getcarrierName();
  },
  methods: {
    isOkClick(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.flag == 1) {
            let url = "/area/addOne";
            let params = {
              carrierOperatorId: this.ruleForm.carrierOperatorId, //运营商id
              areaCode: this.ruleForm.areaCode,
              city: this.ruleForm.city,
              content: this.ruleForm.content
            };
            this.httpPost(url, params, res => {
              if (res.code == 200) {
                this.getList();
                this.dialogVisible = false;
              }
            });
          } else {
            let url = "/area/editOne";
            let params = {
              id: this.editId,
              carrierOperatorId: this.carrierOperatorId, //运营商id
              areaCode: this.ruleForm.areaCode,
              city: this.ruleForm.city,
              content: this.ruleForm.content
            };
            this.httpPut(url, params, res => {
              if (res.code == 200) {
                this.getList();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          console.log("校验失败");
        }
      });
    },
    //分页
    handleCurrentChange(val) {
      this.val = val;
      this.getList();
      console.log(`当前页: ${val}`);
    },
    //新增
    addArea() {
      this.flag = 1;
      this.dialogVisible = true;
      this.ruleForm = [];
    },
    //编辑
    EditClick(row) {
      this.flag = 2;
      this.dialogVisible = true;
      this.editId = row.id;
      this.ruleForm.carrierOperatorId = row.carrierOperatorId; //运营商id
      this.ruleForm.areaCode = Number(row.areaCode);
      this.ruleForm.city = row.city;
      this.ruleForm.content = row.content;
    },

    //刷新
    Refresh() {
      this.getList();
    },

    //删除
    DeleteClick(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let url = "/area/editOne";
          let params = {
            id: row.id,
            status: 1
          };
          this.httpPut(url, params, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.dialogVisible = false;
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //获取数据
    getList() {
      let url = "/area/getList";
      let params = {
        pageNum: this.val,
        pageSize: 10
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.List = res.data.list;
          this.total = res.data.total;
          this.List.forEach((element, i) => {
            this.List[i].index = (this.val - 1) * 10 + i + 1;
          });
        } 
      });
    },
    //获取运营商名称
    getcarrierName() {
      let url = "/carrier/findList";
      let params = {
        pageNum: "",
        pageSize: ""
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.carrierList = res.data.list;
        } else {
          console.log("查询失败");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.areaContainer {
  .option {
    padding: 10px;
  }
  .row {
    padding: 10px;
    .rowTil {
      display: block;
      padding: 10px 0;
    }
  }
}
</style>
<style>
.areaContainer .el-select {
  width: 100% !important;
}
</style>
